'''
Created on Aug 2, 2012

@author: bc6
'''
import datetime
import csv
from boto.mturk.connection import MTurkConnection
from boto.mturk.question import ExternalQuestion
from boto.mturk.qualification import Qualifications, PercentAssignmentsApprovedRequirement

## The AWS access info
ACCESS_ID = 'AKIAJ7IHBCIJGFIU57PA'
SECRET_KEY = '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'

## This is where you set all the variables related to this HIT
## Make sure to check everything here before running the script

# Sandbox host
HOST = 'mechanicalturk.sandbox.amazonaws.com'

# Production host
#HOST = 'mechanicalturk.amazonaws.com'

# The tag hit url
tag_hit_url = 'http://amos.cse.wustl.edu/mturk/tag_camera?id='
annotation = 929

frame_height_px = 800

# HIT params
params = dict(
                lifetime = datetime.timedelta(days=7),
                max_assignments = 3,
                title = 'Image Tagging',
                description = 'Tag the scene with the appropriate descriptions',
                keywords = ['label', 'scene', 'tag', 'street', 'traffic', 'bike', 'pedestrian', 'car', 'annotation'],
                reward = 0.01,
                duration = datetime.timedelta(minutes=20),
                approval_delay = 60*60*24*7, # this Auto Approval period is 60*60*24*7 = 7 days
                response_groups = ['tagging'],
              )
# Qualification requirement for worker, > 99% is reasonable
qualifications = Qualifications()
qualifications.add(PercentAssignmentsApprovedRequirement(comparator="GreaterThan", integer_value="99"))

def create_tagging_hit(mtc, cam=1):
    q = ExternalQuestion(external_url=('%s%s'%(tag_hit_url,cam)), frame_height=frame_height_px)
    create_hit_rs = mtc.create_hit(question = q,
                                   lifetime = params['lifetime'], 
                                   max_assignments = params['max_assignments'], 
                                   title = params['title'], 
                                   keywords = params['keywords'], 
                                   reward = params['reward'], 
                                   duration = params['duration'], 
                                   approval_delay = params['approval_delay'], 
                                   annotation = ('%s'%cam),
                                   qualifications = qualifications)
    assert(create_hit_rs.status == True)
    return create_hit_rs

def get_all_reviewable_hits(mtc):
    page_size = 50
    hits = mtc.get_reviewable_hits(page_size=page_size)
    print "Total results to fetch %s " % hits.TotalNumResults
    print "Request hits page %i" % 1
    total_pages = float(hits.TotalNumResults)/page_size
    int_total= int(total_pages)
    if(total_pages-int_total>0):
        total_pages = int_total+1
    else:
        total_pages = int_total
    pn = 1
    while pn < total_pages:
        pn = pn + 1
        print "Request hits page %i" % pn
        temp_hits = mtc.get_reviewable_hits(page_size=page_size,page_number=pn)
        hits.extend(temp_hits)
    return hits
 
## Creates a batch of tagging hits
##     start: the starting camera id
##     end: the ending camera id (self included)
##     output: the output file for creating this HIT group, in csv format
def run_tagging_hits(output, start=1, end=1):
    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                          aws_secret_access_key=SECRET_KEY,
                          host=HOST)
    cam_list = []
    with open('static/mturk/ids.txt','rb') as f:
        wholething = f.read()
        cam_list = wholething.split('\r\n')
    f.close()
    cam_list = [item for item in cam_list if item is not '']
    work_list = [item for item in cam_list if int(item) >= start and int(item) <= end]
    
    with open(output,'wb') as f:
        writer = csv.writer(f)
        for each in work_list: 
            rs = create_tagging_hit(mtc, each)
            hit_item = rs.pop()
            # format in csv is: HITId, HITTypeId, camera id
            writer.writerow([hit_item.HITId, hit_item.HITTypeId, each])
    f.close()

## Get results using a file that contains information of a HIT group
def get_results(input):
    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                          aws_secret_access_key=SECRET_KEY,
                          host=HOST)
    hits = None
    with open(input,'rb') as f:
        hits = f.read()
    f.close()
    hits_array = hits.split('\r\n')
    hits_array = [hit for hit in hits_array if not hit == '']
    
    big_list_of_dict = []
    
    for each in hits_array:
        hit_id = each.split(',')[0]
        hit_type_id = each.split(',')[1]
        camera_id = each.split(',')[2]
        assignments = mtc.get_assignments(hit_id)
        for assignment in assignments:
            worker_id = assignment.WorkerId
            cur_dict = {'hitid': hit_id,
                        'hittypeid': hit_type_id,
                        'annotation': camera_id,
                        'workerid': worker_id,
                        }
            for question_form_answer in assignment.answers[0]:
                answers = [[item.qid, item.fields[0]] for item in question_form_answer]
                for answer in answers:
                    cur_dict[('Answers.%s'%answer[0])] = answer[1]
            
    known_tags = set(["people", "street", "intersection", "bikelane", "sidewalk", "trafficlight", "crosswalk", "buildings", "shade", "grass", "trees", "plaza", "bench", "fountain", "water", "park", "school", "snow", "mountain", "skiarea"])

    with open(output, 'wb') as f:    
        writer = csv.writer(output, delimiter='\t')
        writer.writerow()
        

    

def run2():
    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                          aws_secret_access_key=SECRET_KEY,
                          host=HOST)
     
    hits = get_all_reviewable_hits(mtc)
     
    for hit in hits:
        assignments = mtc.get_assignments(hit.HITId)
        for assignment in assignments:
            print "Answers of the worker %s" % assignment.WorkerId
            for question_form_answer in assignment.answers[0]:
                for each in question_form_answer.fields:
                    print "%s" % (each)
            print "--------------------"